package nsu.grandfinal;

import net.daum.mf.map.api.MapPOIItem;
import net.daum.mf.map.api.MapPoint;
import net.daum.mf.map.api.MapView;
import android.app.Activity;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.LinearLayout;
import android.widget.Toast;

public class DaumMapActivity extends Activity implements MapView.OpenAPIKeyAuthenticationResultListener, MapView.MapViewEventListener, MapView.CurrentLocationEventListener, MapView.POIItemEventListener{
	private MapView						mapView;
	private double[]					GeoArrayData;
	private String[]					StringArrayData;
	private MapPoint					point;
	private Location					desLoc;
	private Location					myLoc;
	private String						LOG_TAG;
	private static final int	ENABLE_MY_LOCATION	= Menu.FIRST;
	private static final int	DISABLE_MY_LOCATION	= Menu.FIRST + 1;
	private Intent						sendIntent;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		sendIntent = new Intent(DaumMapActivity.this, UserSetting.class);
		Bundle sendBundle = new Bundle();
		desLoc = new Location("Destination");
		MapPOIItem poiItem = new MapPOIItem();
		Intent getIntent = getIntent();
		Bundle getBundle = getIntent.getExtras();
		StringArrayData = getBundle.getStringArray("StringArrayData");
		GeoArrayData = getBundle.getDoubleArray("GeoArrayData");
		point = MapPoint.mapPointWithGeoCoord(GeoArrayData[0], GeoArrayData[1]);
		desLoc.setLatitude(GeoArrayData[0]);
		desLoc.setLongitude(GeoArrayData[1]);
		Log.i("GeoArrayData[0],GeoArrayData[1]:", GeoArrayData[0] + "," + GeoArrayData[1]);
		sendBundle.putDouble("Lat", GeoArrayData[0]);
		sendBundle.putDouble("Lon", GeoArrayData[1]);
		sendBundle.putString("featureName", StringArrayData[0]);
		sendBundle.putString("sender", "DaumMapActivity");
		sendBundle.putString("address", StringArrayData[2]);
		sendIntent.putExtras(sendBundle);
		LinearLayout linearLayout = new LinearLayout(this);
		mapView = new MapView(this);
		mapView.setDaumMapApiKey("9c63f7b4ab1d00cceef52d74ab821be5ee069c60");
		mapView.setOpenAPIKeyAuthenticationResultListener(this);
		mapView.setMapViewEventListener(this);
		mapView.setCurrentLocationEventListener(this);
		mapView.setPOIItemEventListener(this);
		mapView.setMapType(MapView.MapType.Standard);
		linearLayout.addView(mapView);
		setContentView(linearLayout);
		mapView.setMapCenterPointAndZoomLevel(point, 1, true);
		poiItem.setItemName(StringArrayData[0]);
		poiItem.setMarkerType(MapPOIItem.MarkerType.BluePin);
		poiItem.setShowAnimationType(MapPOIItem.ShowAnimationType.SpringFromGround);
		poiItem.setShowCalloutBalloonOnTouch(true);
		poiItem.setDraggable(false);
		poiItem.setMapPoint(point);
		mapView.addPOIItem(poiItem);
	}
	
	public void onCalloutBalloonOfPOIItemTouched(MapView arg0, MapPOIItem arg1) {
		startActivity(sendIntent);
		finish();
	}
	
	public void onDraggablePOIItemMoved(MapView arg0, MapPOIItem arg1, MapPoint arg2) {}
	
	public void onPOIItemSelected(MapView arg0, MapPOIItem arg1) {}
	
	public void onCurrentLocationDeviceHeadingUpdate(MapView arg0, float arg1) {
		Log.i(LOG_TAG, String.format("MapView onCurrentLocationDeviceHeadingUpdate: device heading = %f degrees", arg1));
	}
	
	public void onCurrentLocationUpdate(MapView arg0, MapPoint arg1, float arg2) {
		MapPoint.GeoCoordinate mapPointGeo = arg1.getMapPointGeoCoord();
		Log.i(LOG_TAG, String.format("MapView onCurrentLocationUpdate (%f,%f) accuracy (%f)", mapPointGeo.latitude, mapPointGeo.longitude, arg2));
		myLoc = new Location("MyLocation");
		myLoc.setLatitude(mapPointGeo.latitude);
		myLoc.setLongitude(mapPointGeo.longitude);
		float distance = desLoc.distanceTo(myLoc);
		Log.i("distance:", distance + "");
		Toast.makeText(DaumMapActivity.this, String.format("목적지와의 거리는 %fM 입니다", distance), Toast.LENGTH_SHORT).show();
	}
	
	public void onCurrentLocationUpdateCancelled(MapView arg0) {}
	
	public void onCurrentLocationUpdateFailed(MapView arg0) {}
	
	public void onMapViewCenterPointMoved(MapView arg0, MapPoint arg1) {}
	
	public void onMapViewDoubleTapped(MapView arg0, MapPoint arg1) {}
	
	public void onMapViewInitialized(MapView arg0) {}
	
	public void onMapViewLongPressed(MapView arg0, MapPoint arg1) {}
	
	public void onMapViewSingleTapped(MapView arg0, MapPoint arg1) {}
	
	public void onMapViewZoomLevelChanged(MapView arg0, int arg1) {}
	
	public void onDaumMapOpenAPIKeyAuthenticationResult(MapView arg0, int arg1, String arg2) {}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(Menu.NONE, ENABLE_MY_LOCATION, Menu.NONE, "내위치보기");
		menu.add(Menu.NONE, DISABLE_MY_LOCATION, Menu.NONE, "내위치끄기");
		return super.onCreateOptionsMenu(menu);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
			case ENABLE_MY_LOCATION:
				mapView.setShowCurrentLocationMarker(true);
				mapView.setCurrentLocationTrackingMode(MapView.CurrentLocationTrackingMode.TrackingModeOnWithoutHeading);
				break;
			case DISABLE_MY_LOCATION:
				mapView.setCurrentLocationTrackingMode(MapView.CurrentLocationTrackingMode.TrackingModeOff);
				mapView.setShowCurrentLocationMarker(false);
				mapView.setMapCenterPointAndZoomLevel(point, 1, true);
			default:
				break;
		}
		return false;
	}
}
